package cn.edu.xmu.oomall.order.dao;

import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.order.mapper.RefundPoMapper;
import cn.edu.xmu.oomall.order.model.bo.Refund;
import cn.edu.xmu.oomall.order.model.po.RefundPo;
import cn.edu.xmu.oomall.order.model.po.RefundPoExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.cloneVo;

/**
 * @author 张浩山
 */
@Repository
public class RefundDao {
    @Autowired
    private RefundPoMapper refundPoMapper;


    /**
     * 增加订单
     */
    public ReturnObject insert(Refund refund) {
        RefundPo refundPo = cloneVo(refund, RefundPo.class);
        try {
            int ret = refundPoMapper.insert(refundPo);
            if (ret == 0) {
                return new ReturnObject(ReturnNo.FIELD_NOTVALID);
            }
            return new ReturnObject(refundPo);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    public ReturnObject selectByTradeSn(String tradeSn) {
        try {
            RefundPoExample example = new RefundPoExample();
            RefundPoExample.Criteria criteria = example.createCriteria();
            criteria.andTradeSnEqualTo(tradeSn);
            List<RefundPo> refundPos = refundPoMapper.selectByExample(example);
            Refund refund = cloneVo(refundPos.get(0), Refund.class);
            return new ReturnObject(refund);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }
}
